package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.Enshrine;
import com.qy.model.Like;
import com.qy.service.LikeService;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("app/like")
public class LikeController {
    @Resource
    private LikeService likeService;

    @PostMapping("/add")
    public Result add(@RequestBody Like like) {
        likeService.save(like);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        likeService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Like like) {
        likeService.update(like);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Like like = likeService.findById(id);
        return ResultGenerator.successResult(like);
    }

    @GetMapping("/list")
    public Result list(PageBean<Like> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Like> list = likeService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //点赞
    @PostMapping("/like")
    public Result like(@RequestBody Like like) {
        if (like.getUser_id() == null || like.getComment_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Condition condition = new Condition(Like.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andCondition("user_id =" + like.getUser_id());
        criteria.andCondition("comment_id =" + like.getComment_id());
        List<Like> list = likeService.findByCondition(condition);
        if (list.size() != 0) {
            throw new ServiceException(Constants.CODE_LIKE_THE_COMMENTS);
        }

        like.setAdd_time(DateUtil.getNowTimestamp());
        likeService.save(like);

        Condition condition2 = new Condition(Like.class);
        Example.Criteria criteria2 = condition2.createCriteria();
        criteria2.andCondition("comment_id =" + like.getComment_id());
        List<Like> list2 = likeService.findByCondition(condition2);

        return ResultGenerator.successResult(list2.size());
    }

    //取消点赞
    @PostMapping("/cancleLike")
    public Result cancleLike(@RequestBody Like like) {
        if (like.getUser_id() == null || like.getComment_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Condition condition = new Condition(Like.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andCondition("user_id =" + like.getUser_id());
        criteria.andCondition("comment_id =" + like.getComment_id());
        List<Like> list = likeService.findByCondition(condition);
        if (list.size() == 0) {
            throw new ServiceException(Constants.CODE_ERR_LIKE_THE_COMMENTS);
        }
        Like like1 = list.get(0);
        likeService.deleteById(like1.getId());

        Condition condition2 = new Condition(Like.class);
        Example.Criteria criteria2 = condition2.createCriteria();
        criteria2.andCondition("comment_id =" + like.getComment_id());
        List<Like> list2 = likeService.findByCondition(condition2);

        return ResultGenerator.successResult(list2.size());
    }
}
